package com.litbw.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import com.litbw.dbbackend.DBContestAdapter;
import com.litbw.dbbackend.DBEntryAdapter;
import com.litbw.dbbackend.DBUserAdapter;
import com.litbw.event.Contest;
import com.litbw.event.Contest.Entry;
import com.litbw.publicobservable.LoginMessage;
import com.litbw.publicobservable.LogoutMessage;
import com.litbw.publicobservable.UpdateProfileMessage;
import com.litbw.user.User;

/**
 * 
 * @author Silvia Chita
 * @edited Trygve Stageberg
 */
public class UserProfilePanel extends AbstractObservablePanel {

	/**
	 * Serial ID for this class
	 */
	private static final long serialVersionUID = -8571784094079808397L;

	/**
	 * The name of this panel.
	 */
	private static final String DEF_NAME = "User Profile";
	
	/**
	 * currently logged in user
	 */
	private User user;
	
	private JPanel panel = new JPanel();

	/**
	 * Create a new EventDetailPanel.
	 */
	public UserProfilePanel() {
		super();
		setLayout(new BorderLayout());
		setName(DEF_NAME);
		panel.setBackground(Color.WHITE);
		
		final JScrollPane scroll = new JScrollPane(panel);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		add(scroll, BorderLayout.CENTER);
		
		addComponents();
	}
	
	/**
	 * Add and setup the sub components necessary for this panel.
	 */
	private void addComponents() {
		panel.removeAll();
		if (user != null) {
			JLabel nameLabel = new JLabel("Name: " + user.getName());
			JLabel emailLabel = new JLabel("Email: " + user.getEmail());
			JLabel addressLabel = new JLabel("Address: " + user.getAddress() + ", " + user.getCity() + ", " + user.getState() + user.getZip());
			JLabel phoneLabel = new JLabel("Phone: " + user.getPhone());
			
			panel.add(nameLabel);
			panel.add(emailLabel);
			panel.add(phoneLabel);
			panel.add(addressLabel);
			switch (user.getRole()) {
			case JUDGE:
				displayJudge(panel);
				break;
			case CONTESTANT:
				displayContestant(panel);
				break;
			case ORGANIZER:
				displayOrganizer(panel);
				break;
			}
			
		} else {
			JLabel errorLabel = new JLabel("Oops, you are not currently logged in. There is no user information to display.");
			panel.add(errorLabel);
		}
		
		add(panel);	
		validate();
	}
	
	
	private void displayOrganizer(JPanel panel) {
//		ArrayList<User> registrants = new ArrayList<User>();
		ArrayList<User> users = DBUserAdapter.getAllUsers();
		int counter = 0;
		JPanel tmp = new JPanel();
		tmp.add(new JLabel("---------------------------------------"));
		tmp.add(new JLabel("Event: 1"));
		tmp.add(new JLabel("---------------------------------------"));
		for (User u : users) {
			JLabel nameLabel = new JLabel("Name: " + u.getName());
			JLabel emailLabel = new JLabel("Email: " + u.getEmail());
			JLabel addressLabel = new JLabel("Address: " + u.getAddress() + ", " + u.getCity() + " " + u.getState() + ", " + u.getZip());
			JLabel phoneLabel = new JLabel("Phone: " + u.getPhone());
			
			tmp.add(nameLabel);
			tmp.add(emailLabel);
			tmp.add(phoneLabel);
			tmp.add(addressLabel);
			tmp.add(new JLabel("----------"));
			counter+=5;
		}
		
		tmp.setLayout(new GridLayout(counter+3, 1));
		panel.add(tmp);
	}

	private void displayContestant(JPanel panel) {
	// Get the entries for a specified user.
	final Entry[] entries = DBEntryAdapter.getEntriesForUser(user.getEmail());
	BufferedImage[] imgs = null;
		if (entries != null) {
			imgs = new BufferedImage[entries.length];
			for (int i = 0; i < entries.length; i++) {
				if (entries[i] != null) {
					imgs[i] = entries[i].getDraft().getImage();
				}
			}
			//print every existing draft
			for (BufferedImage img : imgs) {
				if (img != null) {
					final int newwidth = (int) (img.getWidth() * 0.2);
					final int newheight = (int) (img.getHeight() * 0.2);
					panel.add(new JLabel(new ImageIcon(img.getScaledInstance(newwidth, newheight, 0))));
				}
			}
			
		}
	}

	private void displayJudge(JPanel panel) {
		System.out.println("In method");
		System.out.println("finding contests for judge[" + user.getEmail() + "]");
		Contest[] contests = DBContestAdapter.getContestsByJudge(user.getEmail());
		
		
		
		JPanel tmp = new JPanel();
		int counter = 0;
		System.out.println("contests for judge[name='" + user.getEmail() + "'] = " + contests.length);
		for (Contest c : contests) {
			tmp.add(new JLabel("----------------------------------------------"));
			tmp.add(new JLabel("Contest: " + c.getId()));
			tmp.add(new JLabel("----------------------------------------------"));
			
			ArrayList<Entry> entries = c.getEntries();
			
			for (Entry e : entries) {
				System.out.println("In loop");
				JLabel id = new JLabel("Entry #: " + e.getId());
				JLabel descrip = new JLabel(e.getDescription());
				
				BufferedImage img = e.getDraft().getImage();
				final int newwidth = (int) (img.getWidth() * 0.2);
				final int newheight = (int) (img.getHeight() * 0.2);
				
				tmp.add(id);
				tmp.add(descrip);
				tmp.add(new JLabel(new ImageIcon(img.getScaledInstance(newwidth, newheight, 0))));
				counter++;
			}
			counter++;
		}
		tmp.setLayout(new GridLayout(counter, 3));
		tmp.setBackground(new Color(0,0,0,0));
		panel.add(new JScrollPane(tmp));
	}

	@Override
	public void update(final Observable obs, final Object arg) {
		if (arg instanceof LoginMessage) {
			System.out.println("UserProfile got a login message!");
			user = ((LoginMessage) arg).getUser();
			addComponents();	
		} else if (arg instanceof LogoutMessage) {
			user = null;
			System.out.println("Log out Message!!");
			addComponents();
		} else if (arg instanceof UpdateProfileMessage) {
			addComponents();
		}
	}
	
}
